(function () {
    "use strict";

    angular.module('core').controller('reportFinanceiroCR000012Controller', controller);
    controller.$inject = ['$scope', '$timeout', 'toolsService', 'reportFinanceiroService'];

    function controller($scope, $timeout, toolsService, reportFinanceiroService) {
        $scope.initializeLayout('Análise de Liquidações a Receber');

        //[Inicializa objetos]
        $scope.state = {
            data: [],
            carregando: false,
            vazio: false,
            count: 0,
            filtro: {
                ativo: false,
                args: null,
            }
        };

        //[Inicializa funcões]
        $scope.initialize = function () {
            $scope.clearFilter();
            $scope.refresh();
        };

        $scope.getAll = function () {
            $scope.state.filtro.args.skip = 0;
            $scope.state.carregando = true;
            $scope.state.vazio = false;
            reportFinanceiroService
                .getAllLiquidacaoReceber($scope.state.filtro.args)
                .then(
                function (response) {
                    if (response.Status < 0) {
                        $scope.tools.toastr.error(response.Message, 'Serviço');
                        console.error(response.Exception);
                    } else {
                        angular.copy(angular.fromJson(response.Data), $scope.state.data);

                        /* Início Pivot Table */
                        var dateFormat = $.pivotUtilities.derivers.dateFormat;
                        var sortAs = $.pivotUtilities.sortAs;
                        var tpl = $.pivotUtilities.aggregatorTemplates;
                        var fmt = $.pivotUtilities.numberFormat({ suffix: " °C" });

                        $("#pivot-table").pivotUI(toolsService.flattenArray(response.Data), {
                            hiddenAttributes: [],
                            derivedAttributes: {
                                "Data.Ano": dateFormat("Data", "%y", true),
                                "Data.Mes": dateFormat("Data", "%n", true),
                                "Data.Dia": dateFormat("Data", "%d", true),
                                "Data.DiaSemana": dateFormat("Data", "%w", true),
                            },
                            sorters: {
                                "Data.Mes": sortAs(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]),
                                "Data.DiaSemana": sortAs(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]),
                            },
                            rows: ["Titulo", "Filial.Empresa.Nome", "Tipo"],
                            cols: ["Data.Ano", "Data.Mes"],
                            vals: ["Valor"],
                            aggregatorName: "Sum",
                            rendererName: "Heatmap",
                            renderers: $.extend(
                                $.pivotUtilities.renderers,
                                $.pivotUtilities.c3_renderers,
                                $.pivotUtilities.d3_renderers
                            ),
                        });

                        $scope.tools.closeColunasDinamicas();

                        /* Fim Pivot Table */

                    }
                    $scope.state.carregando = false;
                    $scope.state.vazio = ($scope.state.data == 0);
                },
                function (_error) {
                    $scope.state.carregando = false;
                    $scope.tools.toastr.error('Não foi possível conectar com o(s) servidor(es)');
                    console.error(_error);
                }
                );
        };
        $scope.refresh = function () {
            $scope.getAll();
        };

        $scope.clearFilter = function () {
            $scope.state.filtro.args = {
                chaves: '',
                filtro: null,
                periodoEmissao: {
                    De: toolsService.getHoje(),
                },
                sortExpression: '',
                skip: 0,
                take: 999999
            };
        };
        $scope.toggleFilter = function () {
            $scope.state.filtro.ativo = !$scope.state.filtro.ativo;
        };

        /*Inicializa funções*/
        $scope.initialize();

    }

})();